<?php
class Logger {
 
	private $ClassName;
	private $level;
	private $filename;

	function __construct($ClassName, $level = '',$path='') {
		if ($level !='')  {
			$this->level = $level;
		}
		else {
			$this->level = 'info';
		}
		if($path=='') {
//			$this->filename =  BASE_URI.'shared/log/development.log';
		}
		else {
			$this->filename = $path;
		}
	}

/**
* Message pour debug
* 
* @param string $message
* 
*/
	public function debug($message) {
		 $bt = debug_backtrace();
     	 $caller = array_shift($bt);
/*		 
	foreach($caller as $key => $value)
{
	if (is_array($value))
	{
		foreach($value as $key2 => $value2)
  			echo $key2." : ". $value2 . ",";		
	}
	elseif (is_object($value))
	{
  		echo $key." = instance</BR>";
	}
	else
	{
  		echo $key." = ". $value . "</BR>";
	}
}
*/
		 echo '<BR/> file :'.$caller['file'];
		 echo '<BR/> line :'.$caller['line'];
		 echo '<BR/> class :'.$caller['class'];
		 echo '<BR/> function :'.$caller['function'];
		 echo '<BR/> args :';
		 foreach($caller['args'] as $key => $value)
			echo $key." = ". $value . "</BR>";
		 echo '<BR/>';


//		if($this->level == 'debug') {
		//	$this->write_log($message);
//		}
	}

	public function info($message) {
		/*
		if($this->level == 'info' || $this->level == 'debug' ) {
		   $this->write_log($message);
		}
		*/
	}

	public function warn($message) {
		/*
		if($this->level == 'warn' ||$this->level == 'info' || $this->level == 'debug' ) {
		   $this->write_log($message);
		}
		*/
	}

	public function error($message) {
		/*
		if($this->level == 'error' || $this->level == 'warn' || $this->level == 'info' || $this->level == 'debug' ) {
		   $this->write_log($message);
		}
		*/
	}

	public function request() {
		/*
		$headers = getallheaders();
		$this->write_log("REQUEST:");
		$this->write_log($headers);
		$get_params = $_GET;

		if($get_params) {
			$this->write_log("GET parameters:");
			$this->write_log($get_params);
		}
		$post_params = $_POST;
		if($post_params) {
			$this->write_log("POST parameters:");
			$this->write_log($post_params);
		}
		*/
	}

	public function session() {
		/*
		 $session_params = $_SESSION;
		 if($session_params) {
			$this->write_log("SESSION parameters:");
			$this->write_log($session_params);
		}
		*/
	}

	private function write_log($message) {
	//$message can either be a string or an array
		$fd = fopen($this->filename, 'a');
		if(is_array($message)) {
			$this->write_array($message, $fd);
		}
		else {
			$this->write_string($message, $fd);
		}
		fclose($fd);
	}

	private function write_string($message, $fd)  {
		 fwrite($fd, $message."\n");
	}

	private function write_array($message, $fd) {
		 foreach($message as $key => $value) {
			if(is_array($value)) {
			   fwrite($fd, $key."{ ");
			   $this->write_array($value, $fd);
			   fwrite($fd, " }\n");
			}
			else {
				$string =  "\t {".$key.': '.$value."}\n ";
				fwrite($fd, $string);
			}
		}
	}
}

?>
